from flask import render_template, request, flash
from flask_login import current_user

from . import account
from .. import db
from ..models import UserInfo, OAuth
# from .forms import UserBasicDataForm

@account.route('/account/basic', methods=['GET', 'POST'])
def basic():
    if request.method == 'POST':
        new_screenname = request.form.get('screenname')
        new_desc = request.form.get('description')
        if new_screenname:
            user_info = UserInfo.query.filter_by(id=current_user.id).first()
            user_info.screenname = new_screenname
            user_info.desc = new_desc
            db.session.add(user_info)
            db.session.commit()
            flash('更新成功！')
        else:
            flash('用户名不能为空！')
    oauth_count = OAuth.query.filter_by(user_id=current_user.id).count()
    if oauth_count > 1:
        only_one_bind = False
    else:
        only_one_bind = True
    return render_template('account/basic.html', only_one_bind=only_one_bind)
